package cn.tedu_two;

import java.io.*;

public class IOUtils {
    /**
     * 用来关闭读写的资源并释放与此流有关的所有系统资源
     * 参数:
     * Closeable体现了多态,可以接受任何类型的读入流和写出流
     */

    public static void close(Closeable a){
        if(a!=null){
            try{
                a.close();
            }catch(IOException x){
                x.printStackTrace();
            }
        }
    }
    /**
     * 完成文件复制
     * 参数:
     * from源文件 的路径
     * to目标文件 的路径
     */
    public static void copy(String from, String to){

        InputStream in = null;
        OutputStream out =null;
        try {
            in = new BufferedInputStream(new FileInputStream(from));
            out = new BufferedOutputStream(new FileOutputStream(to));
            byte[] z=new byte[8129];
            int a = 0;
            while ((a = in.read(z)) != -1)
                out.write(z);
        }catch(IOException x){
            x.printStackTrace();
        } finally{
            close(in);
            close(out);
        }


    }
}
